package com.asia5b.a5mall.middleware.utils

import android.text.TextUtils
import com.asia5b.a5mall.middleware.model.member.CountryAllModel
import com.asia5b.a5mall.middleware.model.member.MemberModel
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Member

/**
 * Created by zhangyuncai on 2020/4/2.
 */
object PreferenceHelper {
    var cacheUserInfoText by Preference("user3", "")
    var countryAllModelText by Preference("countryAllModel2", "")
    fun seriToMember(): MemberModel {
        val json = cacheUserInfoText
        if (TextUtils.isEmpty(json)) {
            return MemberModel()
        }
        val type = object : TypeToken<MemberModel>() {}.type
        return JsonUtil.jsonToAny<MemberModel>(json, type)!!
    }

    fun memberToSeri(model: MemberModel) {
        cacheUserInfoText = JsonUtil.anyToJson(model)
    }

    /*------------------------------------------------------*/
    fun seriToCountry(): CountryAllModel {
        val json = countryAllModelText
        if (TextUtils.isEmpty(json)) {
            return CountryAllModel()
        }
        val type = object : TypeToken<CountryAllModel>() {}.type
        return JsonUtil.jsonToAny<CountryAllModel>(json, type)!!
    }

    fun countryToSeri(model: CountryAllModel) {
        countryAllModelText = JsonUtil.anyToJson(model)
    }
}